<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Repositories\AccountRepository;
use App\Models\Account;
use App\Traits\BaseFun;

class IndexController extends Controller
{
    //
    public function __construct()
    {

    }

    public function index(){
        return view('admin.index');
    }

    public function dologin(Request $request){
        $userInfo=Account::where('account',$request->post('account'))->where('pass',md5($request->post('pass')))->first();
        if($userInfo['state']==1){
            $Token=md5(time().rand(1000,9999));
            $login_token=Account::where('id',$userInfo['id'])->update(['login_token'=>$Token,'token_time'=>time()]);
            $userInfo=Account::where('account',$request->post('account'))->where('pass',md5($request->post('pass')))->first();
            $dataArr['token']=$Token;
            $dataArr['data']=$userInfo;
            $dataArr['code']=0;
            return BaseFun::jsonReturn($dataArr);
        }elseif($userInfo['state']==2){
            $dataArr['data']=$userInfo;
            $dataArr['code']=2;
            $dataArr['msg']='该帐号已被禁用!';
            return BaseFun::jsonReturn($dataArr); 
        }else{
            $dataArr['data']=$userInfo;
            $dataArr['code']=2;
            $dataArr['msg']='帐号或密码错误!';
            return BaseFun::jsonReturn($dataArr); 
        }
    }

    public function upload_images(Request $request)//上传图片,多图上传
	{
		$file = $_FILES["file"];
		if ($file==null) {
			exit(json_encode(array('code'=>1, 'msg'=>'未上传图片')));
		}
		// 获取文件后缀
		$temp = explode(".", $_FILES["file"]["name"]);
		$extension = end($temp);
		// 判断文件是否合法
		if(!in_array($extension, array("gif","jpeg","jpg","png"))){
			exit(json_encode(array('code'=>1, 'msg'=>'上传图片不合法')));
		}
		//创建文件夹获取当前年月日
		$folder="/upload/images/".date('Y-m-d');

		if(!is_dir('.'.$folder)){
			mkdir('.'.$folder,0777);//创建年+月的文件夹权限为最高
		}
        $file_name = $this->GetRandStr();
		// $info = move_uploaded_file($file['tmp_name'], '.'.$folder.'/'.$file['name']); //移动文件到指定目录 建议文件名自己在修改一下
		$info = move_uploaded_file($file['tmp_name'], '.'.$folder.'/'.$file_name.".".$extension); //移动文件到指定目录 建议文件名自己在修改一下
		$img = $folder.'/'.$file_name.".".$extension;
		// exit(json_encode(array('code'=>0, 'msg'=>$img)));
        $res=['imgurl'=>$img];
        $ArrDate = ['code' => 0, 'data' => $res, 'token' => $request->input('token')];
        return BaseFun::jsonReturn($ArrDate);
	}
    public function upload_mp4_pdf(Request $request)//上传图片,多图上传
	{
		$file = $_FILES["file"];
		if ($file==null) {
			exit(json_encode(array('code'=>1, 'msg'=>'未上传文件')));
		}
		// 获取文件后缀
		$temp = explode(".", $_FILES["file"]["name"]);
		$extension = end($temp);
		// 判断文件是否合法
		if(!in_array($extension, array("mov","mp4","pdf","doc","docx"))){
			exit(json_encode(array('code'=>1, 'msg'=>'上传文件不合法')));
		}
		//创建文件夹获取当前年月日
		$folder="/upload/images/".date('Y-m-d');

		if(!is_dir('.'.$folder)){
			mkdir('.'.$folder,0777);//创建年+月的文件夹权限为最高
		}
        $file_name = $this->GetRandStr();
		// $info = move_uploaded_file($file['tmp_name'], '.'.$folder.'/'.$file['name']); //移动文件到指定目录 建议文件名自己在修改一下
		$info = move_uploaded_file($file['tmp_name'], '.'.$folder.'/'.$file_name.".".$extension); //移动文件到指定目录 建议文件名自己在修改一下
		$img = $folder.'/'.$file_name.".".$extension;
		// exit(json_encode(array('code'=>0, 'msg'=>$img)));
        $res=['imgurl'=>$img];
        $ArrDate = ['code' => 0, 'data' => $res, 'token' => $request->input('token')];
        return BaseFun::jsonReturn($ArrDate);
	}

    public function GetRandStr($length=6){
        $str = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
        $len = strlen($str)-1;
        $randstr = '';
        for ($i=0;$i<$length;$i++) {
            $num=mt_rand(0,$len);
            $randstr .= $str[$num];
        }
        return $randstr;
    }
}
